<?php
/**
 * 			XMLRPC Controller
 * @version			1.0.0
 * @package			XMLRPC for Joomla!
 * @copyright			Copyright (C) 2007-2011 Joomler!.net. All rights reserved.
 * @license			http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 *
 * @author			Yoshiki Kozaki : joomlers@gmail.com
 * @link			http://www.joomler.net/
 *
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die;

class XMLRPCController extends JController
{
	public function service()
	{
		$app = JFactory::getApplication();

		$params = JComponentHelper::getParams('com_xmlrpc');

		$plugin = $params->get('plugin', 'movabletype');

		JPluginHelper::importPlugin('xmlrpc', strtolower($plugin));
		$allCalls = $app->triggerEvent('onGetWebServices');
		if(count($allCalls) < 1){
			JError::raiseError(404, JText::_('COM_XMLRPC_SERVICE_WAS_NOT_FOUND'));
		}

		$methodsArray = array();

		foreach ($allCalls as $calls) {
			$methodsArray = array_merge($methodsArray, $calls);
		}

		@mb_regex_encoding('UTF-8');
		@mb_internal_encoding('UTF-8');

		require_once dirname(__FILE__).DS.'libraries'.DS.'phpxmlrpc'.DS.'xmlrpc.php';
		require_once dirname(__FILE__).DS.'libraries'.DS.'phpxmlrpc'.DS.'xmlrpcs.php';
		require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');
		JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_content/tables');

		$xmlrpc = new xmlrpc_server($methodsArray, false);
		$xmlrpc->functions_parameters_type = 'phpvals';

		$encoding = 'UTF-8';

		$xmlrpc->xml_header($encoding);
		$GLOBALS['xmlrpc_internalencoding'] = $encoding;
		$xmlrpc->setDebug($params->get('debug', JDEBUG));
		@ini_set( 'display_errors', $params->get('display_errors', 0));

		$data = file_get_contents('php://input');

		if(empty($data)){
			JError::raiseError(403, JText::_('COM_XMLRPC_INVALID_REQUEST'));
		}

		$xmlrpc->service($data);

		jexit();
	}

	public function weblayout($preview=false)
	{
		require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');

		$model = $this->getModel('Template');
		$this->addViewPath(JPATH_SITE.'/components/com_content/views');
		$view = $this->getView('Article', 'html', 'ContentView');
		$view->setModel($model, true);
		$doc = JFactory::getDocument();
		$view->assignRef('document', $doc);
		$view->addTemplatePath(JPATH_SITE.'/components/com_content/views/article/tmpl');
		$view->display();
		$view->document->setMetaData('title', '');
		return;
	}

	public function webpreview()
	{
		$this->weblayout(true);
	}
	private function doLogin($username,$password)
	{
		$db =    JFactory::getDBO();
		$query = "SELECT * FROM #__users";
		$db->setQuery($query);
		$results = $db->loadObjectList();
		$success=false;
		$count=count($results);
		for($i=0;$i<count($results);$i++)
		{
			$row = $results[$i];
			if($row->username==$username)
			{
				$arr=explode(':',$row->password);
				$crypt=$arr[0];
				$salt=@$arr[1];
				$getcrypt=JUserHelper::getCryptedPassword($password, $salt);
				if($crypt==$getcrypt)
				{
					break;
				}
			}
		}
		if($i<$count)
		{
			return $row;
		}
		return null;
		
	}
	public function login()
	{
		$username=$_POST['USERNAME'];
		$password=$_POST['PASSWORD'];
		//$username=$_GET['USERNAME'];
		//$password=$_GET['PASSWORD'];
		$f_result=$this->doLogin($username,$password);
		if($f_result!=null)
		{
			echo xmlrpc_encode(true);
		}
		else
		{
			echo xmlrpc_encode(false);
		}
	}
	
	public function getUserInfo()
	{
		$username=$_POST['USERNAME'];
		$password=$_POST['PASSWORD'];
		//$username=$_GET['USERNAME'];
		//$password=$_GET['PASSWORD'];
		$loginResult=$this->doLogin($username,$password);
		if($loginResult!=null)
		{
			$db2 =    JFactory::getDBO();
			$query1 = "SELECT * FROM jos_vquote_extra_info";
			$db2->setQuery($query1);
			$results = $db2->loadObjectList();
			for($i=0;$i<count($results);$i++)
			{
				if($results[$i]->userid==$loginResult->id)
				{
					break;
				}
			}
			$extra=null;
			$extra=json_decode($results[$i]->extra);
			$rsmap=array('id'=>$loginResult->id,'name'=>$loginResult->name,'type'=>$results[$i]->type,'phone'=>$extra->phone,'address'=>$extra->address);
			echo xmlrpc_encode($rsmap);
		}
		else
		{
				$rsmap=array('id'=>'','name'=>'','type'=>'','phone'=>'','address'=>'');
				echo xmlrpc_encode($rsmap);
		}
	}
	
	public function test()
	{
			$db =    JFactory::getDBO();
			$query = "SELECT * FROM jos_vquote_extra_info";
			$db->setQuery($query);
			$results = $db->loadObjectList();
			//echo $results[0]->id."-".$results[0]->name."-".$results[0]->value;
			echo count($results);
	}
	
	public function genPassword()
	{
		$plain=$_GET['plain'];
		$salt=$_GET['salt'];
		echo $plain;
		$getcrypt=JUserHelper::getCryptedPassword($plain);
		echo "<br />";
		echo $getcrypt;
	
	}
	public function getAllCountry()
	{
		$username=$_POST['USERNAME'];
		$password=$_POST['PASSWORD'];
		//$username=$_GET['USERNAME'];
		//$password=$_GET['PASSWORD'];
		$loginResult=$this->doLogin($username,$password);
		if($loginResult!=null)
		{
			$db=JFactory::getDBO();
			$query="SELECT * FROM #__vquote_country";
			$db->setQuery($query);
			$results=$db->loadObjectList();
			$rs="[";
			for($i=0;$i<count($results);$i++)
			{
				$obj="{";
				$obj=$obj."\"id\":\"".$results[$i]->id."\",";
				$obj=$obj."\"countrycode\":\"".$results[$i]->countrycode."\",";
				$obj=$obj."\"countryname\":\"".$results[$i]->countryname."\"}";
				$rs=$rs.$obj.",";
			}
			if($i>0)
			{
				$rs=substr($rs,0,strlen($rs)-1);
			}
			$rs=$rs."]";
			echo xmlrpc_encode($rs);
		}
		else
		{
			echo xmlrpc_encode(null);
		}
	}
	
	public function getStateOfCountry()
	{
		$username=$_POST['USERNAME'];
		$password=$_POST['PASSWORD'];
		$countryid=$_POST['COUNTRYID'];
		//$username=$_GET['USERNAME'];
		//$password=$_GET['PASSWORD'];
		//$countryid=$_GET['COUNTRYID'];
		$loginResult=$this->doLogin($username,$password);
		if($loginResult!=null)
		{
			$db=JFactory::getDBO();
			$query="SELECT * FROM #__vquote_state";
			$db->setQuery($query);
			$results=$db->loadObjectList();
			$rs="[";
			for($i=0;$i<count($results);$i++)
			{
				if($results[$i]->countryid==$countryid)
				{
					$obj="{";
					$obj=$obj."\"id\":\"".$results[$i]->id."\",";
					$obj=$obj."\"regionid\":\"".$results[$i]->regionid."\",";
					$obj=$obj."\"statecode\":\"".$results[$i]->statecode."\",";
					$obj=$obj."\"statenumber\":\"".$results[$i]->statenumber."\",";
					$obj=$obj."\"statename\":\"".$results[$i]->statename."\",";
					$obj=$obj."\"countryid\":\"".$results[$i]->countryid."\"}";
					$rs=$rs.$obj.",";
				}
			}
			if($i>0)
			{
				$rs=substr($rs,0,strlen($rs)-1);
			}
			$rs=$rs."]";
			echo xmlrpc_encode($rs);
		}
		else
		{
			echo xmlrpc_encode(null);
		}
	}
	public function getRegionOfState()
	{
		$username=$_POST['USERNAME'];
		$password=$_POST['PASSWORD'];
		$stateid=$_POST['STATEID'];
		//$username=$_GET['USERNAME'];
		//$password=$_GET['PASSWORD'];
		//$stateid=$_GET['STATEID'];
		$loginResult=$this->doLogin($username,$password);
		if($loginResult!=null)
		{
			$db=JFactory::getDBO();
			$query="SELECT * FROM #__vquote_region";
			$db->setQuery($query);
			$results=$db->loadObjectList();
			$rs="[";
			for($i=0;$i<count($results);$i++)
			{
				if($results[$i]->stateid==$stateid)
				{
					$obj="{";
					$obj=$obj."\"id\":\"".$results[$i]->id."\",";
					$obj=$obj."\"regionname\":\"".$results[$i]->regionname."\",";
					$obj=$obj."\"countryid\":\"".$results[$i]->countryid."\",";
					$obj=$obj."\"stateid\":\"".$results[$i]->stateid."\"}";
					$rs=$rs.$obj.",";
				}
			}
			if($i>0)
			{
				$rs=substr($rs,0,strlen($rs)-1);
			}
			$rs=$rs."]";
			echo xmlrpc_encode($rs);
		}
		else
		{
			echo xmlrpc_encode(null);
		}
		
	}
	public function postRequest()
	{
		$username=$_POST["USERNAME"];
		$password=$_POST["PASSWORD"];
	}
	public function testArray()
	{
		$map=array(9);
		for($i=0;$i<5;$i++)
		{
			$map=array_push($map,$i);
		}
		echo $map;
	}
}